package Wallpaper::Execute::Mangle;
$VERSION = sprintf "%.3f", 1 + substr(q$Revision: 162 $,9)/1000;

package Wallpaper::Execute;
use Image::Magick;

use Wallpaper::Configuration qw( %datastore );
use Wallpaper::Tools;


sub domangle {
    my ( $self, $display, $image ) = @_;
    my $general_config = Wallpaper::Configuration->general;
    my $display_config = Wallpaper::Configuration->display( $display );
    my $mangle_config  = Wallpaper::Configuration->mangle( $display );
    my $debug          = $general_config->{ 'debug' };
    my $this_function  = ( caller( 0 ) )[3];

    my $manglefilename = $image->Get( 'filename' );

    print "$self : $this_function (2) Performing Imange Mangle\n" if ( $debug ge "2" );

    # This is a bit extreme, i know!
    my ( $height, $width, $original, $res_width, $res_height, $cropped_width, $cropped_height );
    my ( $cropped_width_noborder, $cropped_height_noborder, $double_border, $border_geo, $chop_geo );
    my ( $widthcut, $heightcut, $chop );
    my ( $backtile, $nextfiller, $backcolor, $bordercolor );

    ( $res_width, $res_height ) = $display_config->{ 'resolution' } =~ /^(\d+)x(\d+)/;

    print "$self : $this_function (3) Resolution set to : " . $res_width . "x" . $res_height . "\n" if ( $debug ge "3" );

    # The size of the cropped image (with borders)
    $cropped_width  = $res_width - $mangle_config->{ 'rbar_width' };
    $cropped_height = $res_height - $mangle_config->{ 'tbar_height' };

    print "$self : $this_function (3) Cropped image size : " . $cropped_width . "x" . $cropped_height . "\n" if ( $debug ge "3" );

    # The size of the cropped image (without borders)
    $cropped_width_noborder  = $cropped_width - $mangle_config->{ 'bordersize' };
    $cropped_height_noborder = $cropped_height - $mangle_config->{ 'bordersize' };

    print "$self : $this_function (3) Cropped noborder size : " . $cropped_width_noborder . "x" . $cropped_height_noborder . "\n"
      if ( $debug ge "3" );

    $double_border = $mangle_config->{ 'bordersize' } + $mangle_config->{ 'bordersize' };

    $border_geo = $mangle_config->{ 'bordersize' } . "x" . $mangle_config->{ 'bordersize' };
    $chop_geo   = $double_border . "x" . $double_border . "+0+" . $cropped_height;

    print "$self : $this_function (3) Border Geometry : " . $border_geo . "\n" if ( $debug ge "3" );

    die( "Cannot read generic tile image : " . $general_config->{ 'bgdir' } . "/backtile.jpg" )
      if ( !-e $general_config->{ 'bgdir' } . '/backtile.jpg' );
    $image->Read( $general_config->{ 'bgdir' } . '/backtile.jpg' );    # [1] Read in conky background tile image
    die( "Cannot read blank image : " . $general_config->{ 'bgdir' } . "/blank.png" ) if ( !-e $general_config->{ 'bgdir' } . '/blank.png' );
    $image->Read( $general_config->{ 'bgdir' } . '/blank.png' );       # [2] Read in placeholder for background (1x1 tranparent png)

    $original   = $image->[0];
    $backtile   = $image->[1];
    $background = $image->[2];
    $background->Resize( geometry => "!" . $res_width . "x" . $res_height );

    # Always insist on the full photo (top to bottom) fitting.  If width isn't
    # enough after this scale, image will be further expanded.
    $original->Resize( geometry => "10000x" . $cropped_height );       # Width set impossibly high, so we get enough

    $width = $original->Get( 'width' );
    if ( $width < $cropped_width ) {
        print "$self : $this_function (2) Width needs to be adjusted, increasing width to $cropped_width\n" if ( $debug ge "2" );
        $original->Resize( geometry => $cropped_width . "x10000" );    # Height set impossibly high, so we get enough
    }

    $height = $original->Get( 'height' );                              # Reset
    $width  = $original->Get( 'width' );                               # Reset

    print "$self : $this_function (2) Resized image is $width x $height\n" if ( $debug ge "2" );
    $widthcut  = $width - $cropped_width_noborder;
    $heightcut = $height - $cropped_height_noborder;

    if ( $manglefilename =~ /Right_.*/ ) {
        $chop = $widthcut;
        print "$self : $this_function (2) Image filename says to preserve right area, cutting $chop pixels from the LEFT side\n" if ( $debug ge "2" );
    } elsif ( $manglefilename =~ /Left_.*/ ) {
        $chop = 0;
        print "$self : $this_function (2) Image filename says to preserve left area, cutting $chop pixels from the RIGHT side\n" if ( $debug ge "2" );
    } elsif ( $manglefilename =~ /Center_.*/ ) {
        $chop = $widthcut / 2;
        print "$self : $this_function (2) Image filename says to preserve Center, cutting $chop pixels from the RIGHT and LEFT sides\n"
          if ( $debug ge "2" );
    } else {
        $chop = $widthcut;
        print "$self : $this_function (2) Image does not indicate area to save, cutting $chop pixels from the LEFT side\n" if ( $debug ge "2" );
    }
    $original->Crop( geometry => $cropped_width . "x" . $cropped_height . "+" . $chop . "+0" );

    my $colorscheme = $datastore{ $display }{ 'scheme' };

    $backcolor   = "#" . $colorscheme->{ 'primary' }{ 'base' };    # Fill color = picture mean
    $bordercolor = "#" . $colorscheme->{ 'primary' }{ 'dark' };    # Border color is a slightly darker picture mean

    $original->Border( fill => $bordercolor, geometry => $border_geo );    # Add a border to cropped photo
    $original->Chop( geometry => $chop_geo );                              # Now cut bottom/left borders off.

    $background->Texture( Texture => $backtile );                          # Fill background with background tile
    $background->Colorize( fill    => $backcolor,                          # Overlay background color, obscuring *most* of the tile
                           opacity => '92'                                 # makes for a nice subtle texture effect.
                         );
    $background->Composite( image   => $original,                          # Plaster original photo on our prepared background.
                            compose => 'atop',
                            gravity => 'SouthWest'
                          );

    shift( @{ $image } );                                                  # Clean up
    shift( @{ $image } );                                                  # Clean up

  return $background;

} ## end sub domangle

1;
